package cn.lanqiao.jdbc;

import cn.lanqiao.dao.AccountDao;
import cn.lanqiao.jdbc.pojo.Account;
import cn.lanqiao.service.AccountService;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class TemplateTest {
    @Autowired
    private AccountDao dao;
    @Autowired
    private AccountService service;
    @Test
    public void templateTest(){
        // 创建数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/lanqiao");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        // 获取链接
        // 创建JdbcTemplate
        JdbcTemplate template = new JdbcTemplate(dataSource);
        String sql = "insert into account(accname,balance) values(?,?)";
        Account account = new Account("admin",10000);
        template.update(sql,account.getAccname(),account.getBalance());
    }

    @Test
    public void templateTest2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        JdbcTemplate template = context.getBean(JdbcTemplate.class);
        Account account = new Account("admin3",10002);
        String sql = "insert into account(accname,balance) values(?,?)";
        template.update(sql,account.getAccname(),account.getBalance());
    }

    @Test
    public void insertTest(){
        Account account = new Account("lucy",20000);
        dao.insert(account);
    }
    @Test
    public void updateTest(){
        Account account = new Account(4,"lucy",20002);
        dao.update(account);
    }
    @Test
    public void deleteTest(){
        dao.delete(1);
    }
    @Test
    public void selectOneTest(){
        Account account = dao.selectOne(4);
        System.out.println("account = " + account);
    }
    @Test
    public void selectAllTest(){
        List<Account> accList = dao.selectAll();
        System.out.println(accList);
    }

    @Test
    public void countTest(){
        System.out.println(dao.count());
    }

    @Test
    public void transferTest(){
        service.Transfer("lucy","admin2",1000);
    }
    @Test
    public void update(){
        System.out.println("新增方法");
    }
}
